<?php

namespace App\Console\Commands;

use App\Models\SellOrderItem;
use Illuminate\Console\Command;
use DB;
use EasyWeChat;

class SellOrderCheckBack extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'book-shop:sell-order-check-back';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '计算超过72小时未取回退回的图书';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("开始获取...");

        $orders = DB::table('sell_orders')->where('can_back',0)->where('state',5)->get();
        if(!empty($orders))
        {
            foreach ($orders as $v)
            {
                //计算能否取回退回的书
                $now = time();
                $time = $now - $v->platform_success_time;
                $end_time = $v->platform_success_time + 72*3600;
                if($time > 72*3600)
                {
                    DB::table('sell_orders')->where('id',$v->id)->update([
                        'can_back'      => 1,
                        'updated_at'    => $now
                    ]);
                }

                //未发送提醒消息
                if(!$v->can_back_sms)
                {
                    if($time >= 48*3600 && $time <= 72*3600)
                    {
                        //计算审核未通过的图书数量
                        $back_book_num = SellOrderItem::query()->where('sell_order_id',$v->id)->where('state',2)->where('first_check_state',1)->count();
                        if($back_book_num > 0)
                        {
                            $sms = app('easysms');
                            try {
                                $sms->send($v->deliver_mobile,[
                                    'template' => 'SMS_163432657',
                                    'data' => [
                                        'name'      => $v->deliver_name,
                                        'number'    => $back_book_num,
                                        'time'      => date('Y-m-d H:i:s',$end_time),
                                    ],
                                ]);
                            } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
                                $message = $exception->getException('aliyun')->getMessage();
                                //dd($message);
                            }

                            $user = DB::table('users')->where('id',$v->user_id)->first();
                            $officialAccount = EasyWeChat::officialAccount(); // 公众号
                            $wechat_user = \DB::table('wechat_users')->where('unionid',$user->unionid)->first();
                            if(!empty($wechat_user))
                            {
                                $openid = $wechat_user->openid;

                                $first = "被拒收的书，待取回\r\n"."你还有{$back_book_num}本被拒收的书没有取回，取回时间期限将于明天过期，过期后被拒收的书将无法再取回。\r\n";

                                $officialAccount->template_message->send([
                                    'touser'        => $openid,
                                    'template_id'   => 'hLqheQzHCT4WUgqHTp5QIeRSRFV4IS3IAf7GyRb-HX4',
                                    'url'  => '',
                                    'miniprogram' => [
                                        'appid'     => config('wechat.mini_program.default.app_id'),
                                        'path'  => '/pages/sale/order/order-detail/order-detail?id='.$v->id,
                                    ],
                                    'data' => [
                                        'first'      => $first,
                                        'keyword1'   => $v->no,
                                        'keyword2'   => date('Y-m-d H:i:s',$end_time)."\r\n",
                                        'remark'     => '在小程序-卖书订单详情中可以选择退回被拒收的书'
                                    ],
                                ]);

                            }
                        }

                        DB::table('sell_orders')->where('id',$v->id)->update([
                            'can_back_sms'  => 1,
                            'updated_at'    => $now
                        ]);
                    }
                }
            }
        }

        $this->info("获取成功...");
    }
}
